package com.itheima.demo06;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/15 9:17
 */
public class Demo_001 {
    public static void main(String[] args) throws InterruptedException {
        //创建一个map集合 ,考虑线程安全问题 ;
        ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
        ArrayList<String> list = new ArrayList<>();
        list.add("C:\\Users\\97622\\Desktop\\ROBIN\\day16.md");
        list.add("C:\\Users\\97622\\Desktop\\ROBIN\\day18.md");
        list.add("C:\\Users\\97622\\Desktop\\ROBIN\\day15.md");
        list.add("C:\\Users\\97622\\Desktop\\ROBIN\\day17.md");
        list.add("C:\\Users\\97622\\Desktop\\ROBIN\\day19.md");
        //创建线程池!
        ThreadPoolExecutor pool = new ThreadPoolExecutor(1,
                1,
                10, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(list.size()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        //线程池核心线程数为1,最大执行线程数为1,线程队列为集合的长度,超过5 秒就删除用过的线程,默认线程工厂,默认拒绝策略;
        //创建一个CountDownLatch对象;参数为等待的线程数量;
        CountDownLatch cdl = new CountDownLatch(list.size());
        //因为每条线程只能复制一个文件 ,所以没次只传递集合中的一个元素;
        for (int i = 0; i < list.size(); i++) {
            MyRunnable01 mr = new MyRunnable01(list.get(i), cdl, map);
            //开启线程;
            pool.submit(mr);
        }

        //让主程序等待其他线程执行完毕;
        cdl.await();
        //遍历map集合,打印每一个元素 ;
        pool.shutdown();
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("文件名:" + key + ";时间:" + value);
        }
        System.out.println("文件传送完成!");

    }
}
